package edums.student.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edums.common.dto.PagingDTO;
import edums.common.util.Utilities;
import edums.employee.dto.StudentDTO;
import edums.student.dao.GuestBookDAO;
import edums.student.dto.GuestBookDTO;

public class GuestBookServiceImpl implements GuestBookService {
	private GuestBookDAO guestBookDAO;
	public GuestBookServiceImpl(GuestBookDAO guestBookDAO){
		this.guestBookDAO = guestBookDAO;
	}
	
	@Override
	public void doWriteGuestBook(GuestBookDTO gdto) throws SQLException{
		gdto.setGuestBookWriteDate(Utilities.getNow());
		int guestBookNo = guestBookDAO.selectGuestBookNo(gdto.getStudentId());
		gdto.setGuestBookNo(guestBookNo);
		gdto.setReplyFamily(guestBookNo);
		gdto.setReplyStep(0);
		gdto.setReplyLevel(0);
		gdto.setGuestBookContent(Utilities.changeContentForDB(gdto.getGuestBookContent()));
		guestBookDAO.insertGuestBook(gdto);
	}
	
	@Override
	public void doRemoveGuestBook(Map guestBookInfo) throws SQLException {
		guestBookDAO.removeGuestBook(guestBookInfo);
	}

	@Override
	public void doWriteReply(GuestBookDTO gdto) throws SQLException{
		gdto.setGuestBookWriteDate(Utilities.getNow());
		int guestBookNo = guestBookDAO.selectGuestBookNo(gdto.getStudentId());
		gdto.setGuestBookNo(guestBookNo);
		gdto.setReplyStep(guestBookDAO.selectReplyRestep(gdto));
		System.out.println(guestBookDAO.selectReplyRestep(gdto));
		gdto.setReplyLevel(1);
		gdto.setGuestBookContent(Utilities.changeContentForDB(gdto.getGuestBookContent()));
		guestBookDAO.insertGuestBook(gdto);
	}

	@Override
	public GuestBookDTO doGetGuestBookById(Map guestBookInfo)
			throws SQLException {
		GuestBookDTO gdto = guestBookDAO.selectGuestBookById(guestBookInfo);
		gdto.setGuestBookWriteDate(Utilities.changeDayTimeFormat(gdto.getGuestBookWriteDate()));
		return gdto;
	}

	@Override
	public GuestBookDTO doGetGuestBookForModifyForm(Map guestBookInfo)
			throws SQLException {
		GuestBookDTO gdto = guestBookDAO.selectGuestBookForModifyForm(guestBookInfo);
		gdto.setGuestBookContent(Utilities.changeContentForTextArea(gdto.getGuestBookContent()));
		return gdto;
	}

	@Override
	public void doModifyGuestBook(GuestBookDTO gdto) throws SQLException {
		gdto.setGuestBookWriteDate(Utilities.getNow());
		gdto.setGuestBookContent(Utilities.changeContentForDB(gdto.getGuestBookContent()));
		guestBookDAO.updateGuestBook(gdto);
	}

	@Override
	public Map doGetAllGuestBook(Map guestBookInfo) throws SQLException {
		ArrayList<GuestBookDTO> gList = (ArrayList<GuestBookDTO>) guestBookDAO.selectAllGuestBook(guestBookInfo);
		ArrayList rList = (ArrayList) guestBookDAO.selectGuestBookReplyCount(guestBookInfo);
		for(GuestBookDTO gdto : gList){
			gdto.setGuestBookWriteDate(Utilities.changeDayTimeFormat(gdto.getGuestBookWriteDate()));
		}
		PagingDTO pagingDTO = new PagingDTO(guestBookDAO.selectGuestBookTotalContent(guestBookInfo), (Integer)guestBookInfo.get("page"));
		Map map = new HashMap();
		map.put("rList", rList);
		map.put("gList", gList);
		System.out.println(gList);
		map.put("pagingDTO", pagingDTO);
		return map;
	}

	@Override
	public List doGetGuestBookReply(Map guestBookInfo) throws SQLException {
		ArrayList<GuestBookDTO> rList = (ArrayList<GuestBookDTO>) guestBookDAO.selectGuestBookReply(guestBookInfo);
		for(GuestBookDTO gdto : rList){
			gdto.setGuestBookWriteDate(Utilities.changeDayTimeFormat(gdto.getGuestBookWriteDate()));
		}
		return rList;
	}

	@Override
	public void doModifyReply(GuestBookDTO gdto) throws SQLException {
		gdto.setGuestBookWriteDate(Utilities.getNow());
		gdto.setGuestBookContent(Utilities.changeContentForDB(gdto.getGuestBookContent()));
		guestBookDAO.updateReply(gdto);
	}

	@Override
	public void doRemoveReply(Map guestBookInfo) throws SQLException {
		guestBookDAO.deleteReply(guestBookInfo);
	}

	@Override
	public StudentDTO doGetStudentInfo(String studentId) throws SQLException {
		return guestBookDAO.selectStudentInfo(studentId);
	}
	
	
}
